<?php
/**
 * Created by : PhpStorm
 * User: godjarvis
 * Date: 2020/8/6
 * Time: 10:21
 */

namespace App\Bucket\Aspect;


use App\Http\Requests\Product\IndexRequest;
use App\Model\Dao\SearchHistoryDao;
use App\Model\Service\UserService;
use Fresh\Foundation\Annotation;
use Fresh\Foundation\Interfaces\InterceptorInterface;
use Fresh\Foundation\Proxy;

/**
 * @Annotation
 * @Target("METHOD")
 */
class SearchLogAspect extends Annotation implements InterceptorInterface
{
    public function preHandle($methodName, $arguments, Proxy $proxy)
    {
        // TODO: Implement preHandle() method.
    }

    public function afterCompletion($result, $methodName, $arguments, Proxy $proxy)
    {
        /** @var IndexRequest $indexRequest */
        $indexRequest = $arguments[0];
        if (!empty($indexRequest->keywords)) {
            $id = SearchHistoryDao::getInstance()->searchTextExistsId($indexRequest->keywords);
            if ($id) {
                SearchHistoryDao::getInstance()->update([], $id);
            } else {
                SearchHistoryDao::getInstance()->insert([
                    'dev_id'      => UserService::getInstance()->getUser()->id,
                    'search_text' => $indexRequest->keywords
                ]);
            }
        }
    }
}